<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020-02-22
 * Time: 13:10
 */
namespace app\cms\controller;
use app\cms\model\Newsclass as NewsclassModel;
use think\Config;
class Newsclass extends \app\admin\controller\Base
{
    public function index(){
        $list = NewsclassModel::where('title','like','%'.$this->request->param('keywords').'%')
            ->order('sort','desc')
            ->paginate(Config::get('paginate')['list_rows']);
        $this->assign('list',$list);
        return $this->fetch();
    }

    public function add(){
        $plist = NewsclassModel::where('pid',0)->where('status',1)->select();
        $this->assign('plist',$plist);
        return $this->fetch();
    }

    public function adddo(){
        $data = array(
            'pid' => $this->request->param('pid'),
            'title' => $this->request->param('title'),
        );
        if(count($data)>=2){
            $check = NewsclassModel::get(['title'=>$data['title']]);
            if($check){
                $res = ['statusCode'=>300,'message'=>'栏目已存在'];
            }else{
                if($insert = NewsclassModel::create($data)){
                    $res = ['statusCode'=>200,'message'=>'操作成功'];
                }else{
                    $res = ['statusCode'=>300,'message'=>'操作失败'];
                }
            }
        }else{
            $res = ['statusCode'=>300,'message'=>'数据录入错误'];
        }
        echo json_encode($res);
    }

    public function edit(){
        $plist = NewsclassModel::where('pid',0)->where('status',1)->select();
        $this->assign('plist',$plist);
        $this->assign('info',NewsclassModel::get($this->request->param('id')));
        return $this->fetch();
    }

    public function editdo(){
        $data = array(
            'id' => $this->request->param('id'),
            'pid' => $this->request->param('pid'),
            'title' => $this->request->param('title'),
        );
        if(count($data)>=3){
            $check = NewsclassModel::where('title',$data['title'])->where('id','<>',$data['id'])->find();
            if($check){
                $res = ['statusCode'=>300,'message'=>'栏目已存在'];
            }else{
                if($update = NewsclassModel::update($data)){
                    $res = ['statusCode'=>200,'message'=>'操作成功'];
                }else{
                    $res = ['statusCode'=>300,'message'=>'操作失败'];
                }
            }
        }else{
            $res = ['statusCode'=>300,'message'=>'数据录入错误'];
        }
        echo json_encode($res);
    }

    public function delete(){
        $ids = explode(',',$this->request->param('ids'));
        $i = 0;
        if(count($ids)>0){
            foreach($ids as $key=>$value){
                $del = NewsclassModel::get($value);
                if($del['status']=='关闭'){
                    NewsclassModel::destroy($value);
                    $i++;
                }
            }
            if($i>0){
                $res = ['statusCode'=>200,'message'=>'操作完成'];
            }else{
                $res = ['statusCode'=>300,'message'=>'请先关闭操作项后在执行此操作'];
            }
        }else{
            $res = ['statusCode'=>300,'message'=>'操作失败'];
        }
        echo json_encode($res);
    }

    public function set(){
        $status = $this->request->param('status');
        $id = $this->request->param('id');
        if($id){
            NewsclassModel::where('id',$id)->update(['status'=>$status]);
            $res = ['statusCode'=>200,'message'=>'操作完成'];
        }else{
            $res = ['statusCode'=>300,'message'=>'非法操作'];
        }
        echo json_encode($res);
    }

    public function sort(){
        $sort = $this->request->param('sort');
        $id = $this->request->param('id');
        if($id){
            NewsclassModel::where('id',$id)->update(['sort'=>$sort]);
            $res = ['statusCode'=>200,'message'=>'操作完成'];
        }else{
            $res = ['statusCode'=>300,'message'=>'非法操作'];
        }
        echo json_encode($res);
    }


}